﻿#region License
// (c) Garry McGlennon 2009
// 
// This project is made available under the Microsoft Public License (Ms-PL)
// Code is provided as is and with no warrenty – Use at your own risk
// 
// View the project and the latest code at http://dnpframework.codeplex.com/
// 
// All other rights reserved.
// 
// blog: www.dotNetProfessional.com/blog/
#endregion
using dnp.Framework.Database.DatabaseExplorer;
using MbUnit.Framework;

namespace dnp.Framework.UnitTest.Database.DatabaseExplorer
{
    [TestFixture]
    public class ViewTest
    {
        private Framework.Database.DatabaseExplorer.Database _database;

        [TestFixtureSetUp]
        public void Setup()
        {
            _database = new Framework.Database.DatabaseExplorer.Database("ReferenceSchema", null);
        }

        [Test]
        public void Name()
        {
            View t = _database.Views["dbo.v_AllDataTypes"];

            Assert.AreEqual("v_AllDataTypes", t.Name);
        }

        [Test]
        public void Alias()
        {
            // Check that aliased View names have thier alias
            Assert.AreEqual("CourseStudent", _database.Views["dbo.v_CourseStudents"].Alias);
            // Check that non-aliased Views have the name of the View
            Assert.AreEqual("v_AllDataTypes", _database.Views["dbo.v_AllDataTypes"].Alias);
        }

        [Test]
        public void FullName()
        {
            View t = _database.Views["dbo.v_AllDataTypes"];
            Assert.AreEqual(t.Schema + "." + t.Name, t.FullName);
        }

        [Test]
        public void Schema()
        {
            Assert.AreEqual("dbo", _database.Views["dbo.v_AllDataTypes"].Schema);
            //Assert.AreEqual("test", _database.Views["test.Dealer"].Schema);
        }

        /// <summary>
        /// Test that the SQL of the view can be extracted
        /// </summary>
        [Test]
        public void Text()
        {
            string expectedSQL=
@"CREATE VIEW dbo.v_CourseStudents
AS
SELECT     dbo.Course.CourseID, dbo.Course.Name, dbo.Person.FirstName, dbo.Person.LastName
FROM         dbo.Course INNER JOIN
                      dbo.StudentCourseLink ON dbo.Course.CourseID = dbo.StudentCourseLink.CourseID INNER JOIN
                      dbo.Student ON dbo.StudentCourseLink.StudentID = dbo.Student.StudentID INNER JOIN
                      dbo.Person ON dbo.Student.StudentID = dbo.Person.PersonID
";

            string actualSQL = _database.Views["dbo.v_CourseStudents"].Text;
            Assert.AreEqual(expectedSQL, actualSQL);
        }
    }
}
